#! /bin/bash

cd /root/remote_vehicle
# 拉取远程更新代码
git pull
cd /root/remote_vehicle/vehicle-dispatching-system
# 编译
mvn clean package -Dmaven.test.skip=true

# 如果复制的目标目录不存在则创建
if [ ! -d "/root/vehicle-dispatching-system/user-80" ]; then
	mkdir -p /root/vehicle-dispatching-system/user-80
fi
if [ ! -d "/root/vehicle-dispatching-system/transaction-8001" ]; then
	mkdir -p /root/vehicle-dispatching-system/transaction-8001
fi
if [ ! -d "/root/vehicle-dispatching-system/chat-8002" ]; then
	mkdir -p /root/vehicle-dispatching-system/chat-8002
fi


# 复制项目jar包到vehicle-dispatching-system目录下
cp -rf /root/remote_vehicle/vehicle-dispatching-system/user-80/target/*.jar /root/vehicle-dispatching-system/user-80/user-80.jar
cp -rf /root/remote_vehicle/vehicle-dispatching-system/user-80/Dockerfile /root/vehicle-dispatching-system/user-80/Dockerfile

cp -rf /root/remote_vehicle/vehicle-dispatching-system/transaction-8001/target/*.jar /root/vehicle-dispatching-system/transaction-8001/transaction-8001.jar
cp -rf /root/remote_vehicle/vehicle-dispatching-system/transaction-8001/Dockerfile /root/vehicle-dispatching-system/transaction-8001/Dockerfile

cp -rf /root/remote_vehicle/vehicle-dispatching-system/chat-8002/target/*.jar /root/vehicle-dispatching-system/chat-8002/chat-8002.jar
cp -rf /root/remote_vehicle/vehicle-dispatching-system/chat-8002/Dockerfile /root/vehicle-dispatching-system/chat-8002/Dockerfile

cp -rf /root/remote_vehicle/vehicle-dispatching-system/docker-compose.yml /root/vehicle-dispatching-system/docker-compose.yml
# 删除之前所有正在运行的容器
docker rm -f $(docker ps -aq)
# 删除旧项目的镜像
docker rmi $(docker images | grep vehicle | awk '{print $3}')
# docker编排启动
cd /root/vehicle-dispatching-system
docker-compose up -d
